package cn.zjyj.module.zjyj.service.devicegroup;

import cn.hutool.core.collection.CollUtil;
import cn.zjyj.framework.datasource.mybatis.core.query.LambdaQueryWrapperX;
import cn.zjyj.module.zjyj.dal.dataobject.device.DeviceDO;
import cn.zjyj.module.zjyj.dal.mysql.device.DeviceMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.zjyj.module.zjyj.controller.admin.devicegroup.vo.*;
import cn.zjyj.module.zjyj.dal.dataobject.devicegroup.DeviceGroupDO;
import cn.zjyj.framework.common.common.pojo.PageResult;
import cn.zjyj.framework.common.common.util.object.BeanUtils;

import cn.zjyj.module.zjyj.dal.mysql.devicegroup.DeviceGroupMapper;

import static cn.zjyj.framework.common.common.exception.util.ServiceExceptionUtil.exception;
import static cn.zjyj.framework.common.common.util.collection.CollectionUtils.convertList;
import static cn.zjyj.module.zjyj.enums.ErrorCodeConstants.*;

/**
 * 设备分组 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class DeviceGroupServiceImpl implements DeviceGroupService {

    @Resource
    private DeviceGroupMapper deviceGroupMapper;
    @Resource
    DeviceMapper deviceMapper;

    @Override
    public Long createDeviceGroup(DeviceGroupSaveReqVO createReqVO) {
        // 插入
        DeviceGroupDO deviceGroup = BeanUtils.toBean(createReqVO, DeviceGroupDO.class);
        deviceGroupMapper.insert(deviceGroup);
        // 返回
        return deviceGroup.getId();
    }

    @Override
    public void updateDeviceGroup(DeviceGroupSaveReqVO updateReqVO) {
        // 校验存在
        validateDeviceGroupExists(updateReqVO.getId());
        // 更新
        DeviceGroupDO updateObj = BeanUtils.toBean(updateReqVO, DeviceGroupDO.class);
        deviceGroupMapper.updateById(updateObj);
    }

    @Override
    public void deleteDeviceGroup(Long id) {
        LambdaQueryWrapperX<DeviceDO> deviceQueryWrapperX = new LambdaQueryWrapperX<DeviceDO>();
        deviceQueryWrapperX.eq(DeviceDO::getGroupId,id);
        Long l = deviceMapper.selectCount(deviceQueryWrapperX);
        if (l > 0) {
            throw exception("该分组已使用，禁止删除");
        }

        // 校验存在
        validateDeviceGroupExists(id);
        // 删除
        deviceGroupMapper.deleteById(id);
    }

    @Override
        public void deleteDeviceGroupListByIds(List<Long> ids) {
        // 校验存在
        validateDeviceGroupExists(ids);
        // 删除
        deviceGroupMapper.deleteByIds(ids);
        }

    private void validateDeviceGroupExists(List<Long> ids) {
        List<DeviceGroupDO> list = deviceGroupMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(DEVICE_GROUP_NOT_EXISTS);
        }
    }

    private void validateDeviceGroupExists(Long id) {
        if (deviceGroupMapper.selectById(id) == null) {
            throw exception(DEVICE_GROUP_NOT_EXISTS);
        }
    }

    @Override
    public DeviceGroupDO getDeviceGroup(Long id) {
        return deviceGroupMapper.selectById(id);
    }

    @Override
    public PageResult<DeviceGroupDO> getDeviceGroupPage(DeviceGroupPageReqVO pageReqVO) {
        return deviceGroupMapper.selectPage(pageReqVO);
    }

}